/* PermissionServiceImpl.java */
package com.example.e_comm.service.ServiceImpl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.e_comm.dto.PermissionDTO;
import com.example.e_comm.entity.Permission;
import com.example.e_comm.mapper.PermissionMapper;
import com.example.e_comm.service.PermissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Transactional
@RequiredArgsConstructor
public class PermissionServiceImpl
        extends ServiceImpl<PermissionMapper, Permission>
        implements PermissionService {
    @Autowired
    private  PermissionMapper permissionMapper;

    @Override
    public boolean create(PermissionDTO dto) {
        Permission p = new Permission();
        p.setPerName(dto.getPermName());
        p.setPermKey(dto.getPermKey());
        return permissionMapper.insert(p) > 0;
    }

    @Override
    public boolean remove(Long id) {
        return permissionMapper.deleteById(id) > 0;   // 逻辑删除，MP全局已配
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectById(id);
    }

    @Override
    public List<Permission> getPermissionsByPage(Page<Permission> page) {
        return permissionMapper.selectList(page, null);
    }

    @Override
    public IPage<Permission> getPermissionsByPageWitchCondition(Page<Permission> page, String keyword) {
        return null;
    }

    @Override
    public IPage<Permission> getPermissionsByPageWithCondition(Page<Permission> page, String keyword) {
        QueryWrapper<Permission> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(keyword)) {
            wrapper.like("perm_name", keyword)
                    .or()
                    .like("perm_key", keyword);
        }
        return permissionMapper.selectPage(page, wrapper);
    }

    @Override
    public List<Permission> getPermissionsByUserId(Long userId) {
        return permissionMapper.getPermissionsByUserId(userId);
    }

    @Override
    public List<Permission> getPermissionsByRoleId(Long roleId) {
        return permissionMapper.getPermissionsByRoleId(roleId);
    }

    @Override
    public Set<String> getPermissionsByUserIdSet(Long userId) {
        List<Permission> permissions = getPermissionsByUserId(userId);
        return permissions.stream()
                .map(Permission::getPermKey)
                .collect(Collectors.toSet());
    }

    @Override
    public Set<String> getPermissionsByRoleIdSet(Long roleId) {
        List<Permission> permissions = getPermissionsByRoleId(roleId);
        return permissions.stream()
                .map(Permission::getPermKey)
                .collect(Collectors.toSet());
    }

    @Override
    public IPage<Permission> getPermissionByPageWithCount(Page<Object> objectPage, String keyword) {
        return null;
    }

    @Override
    public IPage<Permission> getPermissionsByPage(Page<Permission> page, String keyword) {
        return null;
    }

    @Override
    public boolean saveBatch(Collection<Permission> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<Permission> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<Permission> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(Permission entity) {
        return false;
    }

    @Override
    public Permission getOne(Wrapper<Permission> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Optional<Permission> getOneOpt(Wrapper<Permission> queryWrapper, boolean throwEx) {
        return Optional.empty();
    }

    @Override
    public Map<String, Object> getMap(Wrapper<Permission> queryWrapper) {
        return Map.of();
    }

    @Override
    public <V> V getObj(Wrapper<Permission> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public List<Permission> list() {
        return permissionMapper.selectList(
                new LambdaQueryWrapper<Permission>().orderByAsc(Permission::getId));
    }

    @Override
    public PermissionMapper getBaseMapper() {
        return null;
    }

    @Override
    public Class<Permission> getEntityClass() {
        return null;
    }
}